package me.siphone.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.tencent.mmkv.MMKV;
import me.siphone.Constants;
import me.siphone.activity.LoginActivity;
import me.siphone.http.api.QueryUserInfo;
import me.siphone.sip.SipUtil;
import org.linphone.R;
import org.linphone.settings.SettingsActivity;

public class MyFragment extends BaseFragment {

    private TextView tvUsername;
    private TextView tvDept;
    private ImageView ivBack;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my, container, false);

        tvUsername = view.findViewById(R.id.tv_name);
        tvDept = view.findViewById(R.id.tv_dept);
        ivBack = view.findViewById(R.id.iv_back);

        view.findViewById(R.id.logout)
                .setOnClickListener(
                        v -> {
                            MMKV.defaultMMKV().remove(Constants.TOKEN);

                            SipUtil.removeAllAccount();

                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            startActivity(intent);
                        });

        view.findViewById(R.id.setting)
                .setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(getActivity(), SettingsActivity.class));
                            }
                        });

        view.findViewById(R.id.call_history)
                .setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                getFragmentManager()
                                        .beginTransaction()
                                        .replace(R.id.fl_container, new CallHistoryFragment())
                                        .addToBackStack(CallHistoryFragment.class.getSimpleName())
                                        .commit();
                            }
                        });

        getFragmentManager()
                .addOnBackStackChangedListener(
                        new FragmentManager.OnBackStackChangedListener() {
                            @Override
                            public void onBackStackChanged() {
                                int count = getFragmentManager().getBackStackEntryCount();
                                if (count > 0) {
                                    ivBack.setVisibility(View.VISIBLE);
                                } else {
                                    ivBack.setVisibility(View.GONE);
                                }
                            }
                        });

        ivBack.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        getFragmentManager().popBackStack();
                    }
                });

        getUserInfo();

        return view;
    }

    private void getUserInfo() {
        EasyHttp.get(this)
                .api(new QueryUserInfo())
                .request(
                        new HttpCallbackProxy<QueryUserInfo.Result>(this) {
                            @Override
                            public void onHttpSuccess(@NonNull QueryUserInfo.Result result) {
                                super.onHttpSuccess(result);
                                if (result.getUser() != null) {
                                    tvUsername.setText(result.getUser().getNickName());
                                    if (result.getUser().getDept() != null) {
                                        tvDept.setText(result.getUser().getDept().getDeptName());
                                    }
                                }
                            }
                        });
    }
}
